package com.hxkj.controller;

import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Column;
import com.hxkj.domain.po.ColumnTree;
import com.hxkj.service.ColumnService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.platform.common.dispatcher.util.ResultStructUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @ClassName ColumnController
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 11:57
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/column")
public class ColumnController {

    @Autowired
    ColumnService columnService;
    
    /**
    * @author Mr.zheng
    * @Description saveColumn 新增文章分类
    **/
    @RequestMapping(value = "/save", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct> saveColumn(@RequestBody Column column) throws Exception{
        return () -> {
            ResultDataStruct<Column> resultDataStruct = new ResultDataStruct<>();
            try {
                resultDataStruct.setData(columnService.saveColumn(column));
                resultDataStruct.setMessage("成功");
                resultDataStruct.setCode(200);
            }catch (CustomException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("新增文章分类出错",e);
                resultDataStruct.setMessage("新增文章分类出错");
                resultDataStruct.setCode(400);
            }
            return resultDataStruct;
        };
    }


    /**
    * @author Mr.zheng
    * @Description getColumnTreeList 获取文章分类的树结构列表
    * @Date 15:53 2018/6/7
    **/
    @RequestMapping(value = "/getlisttree", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<List<ColumnTree>> getColumnTreeList(@RequestBody Column column) throws Exception{
        return () -> columnService.getColumnTreeList(column);
    }

    /**
     * @author Mr.zheng
     * @Description getColumnTreeList 关键字搜索文章分类树形结构列表
     **/
    @RequestMapping(value = "/getlisttreebykeyword", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<List<ColumnTree>> getColumnTreeListByKeyword(@RequestBody Column column) throws Exception{
        return () -> columnService.getColumnTreeListByKeyword(column);
    }

    /**
    * @author Mr.zheng
    * @Description getShowColumnTreeList 获取文章分类树结构下拉框
    **/
    @RequestMapping(value = "/getshowlisttree", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<List<ColumnTree>> getShowColumnTreeList(@RequestBody Column column) throws Exception{
        return () -> columnService.getShowColumnTreeList(column);
    }

    /**
    * @author Mr.zheng
    * @Description getColumnList 获取文章分了列表
    **/
    @RequestMapping(value = "/getlist", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultPageStruct<Article>> getColumnArticleList(@RequestBody ParameterDataStruct<Column> parameterDataStruct) throws Exception{
        return () -> ResultStructUtils.buildResultPageStruct(columnService.getColumnArticleList(parameterDataStruct.getData() == null ?
                new Column() : parameterDataStruct.getData(), parameterDataStruct.getPageTo()),"param=value");
    }

    /**
     * @author Mr.zheng
     * @Description getColumnList 获取文章分了列表
     **/
    @RequestMapping(value = "/getlistbyterms", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<List<Column>> getColumnListByTerms(@RequestBody Column column) throws Exception{
        return () -> columnService.getColumnListByTerms(column);
    }

    /**
     * @author Mr.zheng
     * @Description removerColumn 删除文章分类
     **/
    @RequestMapping(value = "/remove", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> removeColumn(@RequestBody  Column column) throws Exception{
        return () -> {
            ResultMessageStruct resultStruct = new ResultMessageStruct();
            try {
                columnService.removeColumn(column);
                resultStruct.setMessage("成功");
                resultStruct.setCode(200);
            }catch (CustomException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("删除文章分类出错",e);
                resultStruct.setMessage("删除文章分类出错");
                resultStruct.setCode(400);
            }
            return resultStruct;
        };
    }

    /**
     * @author Mr.zheng
     * @Description modifyColumn 修改文章分类
     * @Date 15:22 2018/6/7
     **/
    @RequestMapping(value = "/modify", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> modifyColumn(@RequestBody Column column) throws Exception{
        return () -> {
            ResultMessageStruct resultStruct = new ResultMessageStruct();
            try {
                columnService.modifyColumn(column);
                resultStruct.setMessage("成功");
                resultStruct.setCode(200);
            }catch (CustomException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("修改文章分类出错",e);
                resultStruct.setMessage("修改文章分类出错");
                resultStruct.setCode(400);
            }
            return resultStruct;
        };
    }

    /**
     * @author Mr.zheng
     * @Description modifySortNum 增加文章分类排序
     * @Date 9:48 2018/6/8
     **/
    @RequestMapping(value = "/modifysortnum", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> modifySortNum(@RequestBody Map<String,Object> map) throws Exception{
        return () -> columnService.modifySortNum(map);
    }

    /**
    * @author Mr.zheng
    * @Description modifyTemptId 修改模板ID
    * @Date 20:38 2018/9/13
    **/
    @RequestMapping(value = "/modifyTemptId", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> modifyTemptId(@RequestBody Column column) throws Exception{
        return () -> {
            ResultMessageStruct resultStruct = new ResultMessageStruct();
            try {
                columnService.modifyTemptId(column);
                resultStruct.setMessage("成功");
                resultStruct.setCode(200);
            }catch (CustomException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("修改模板ID出错",e);
                resultStruct.setMessage("修改模板ID出错");
                resultStruct.setCode(400);
            }
            return resultStruct;
        };
    }

    /**
     * @author zzx
     * @Description getChildren  获取文章分类父类下所有得子类
     * @return
     **/
    @RequestMapping(value = "/getChildren", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<List<Column>> getChildren(@RequestBody Column column) throws Exception{
        return () -> columnService.getChildren(column);
    }

    /**
     * @author zzx
     * @Description getChildren  获取一条文章分类信息
     * @return
     **/
    @RequestMapping(value = "/get", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<Column> getColumn(@RequestBody Column column) throws Exception{
        return () -> columnService.getColumn(column);
    }
    /**
     * @author chenfangzhou
     * @Description 根据分类名称获取一条文章分类
     * @return
     **/
    @RequestMapping(value = "/getcolumnbyname", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<Column> getColumnByName(@RequestBody Column column) throws Exception{
        return () -> columnService.getColumnByName(column);
    }

    /**
     * @author chenfangzhou
     *@Description 根据文章分类id获取该分类的最顶级分类对象
     * @throws Exception
     */
    @RequestMapping(value = "/getTopParentColumn", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public List<Column> getTopParentColumn(@RequestBody Column column) throws Exception{
        return columnService.getTopParentColumn(column);
    }


    /**
     * @author chenfangzhou
     * @Description 根据分类名称获取一条文章分类
     * @return
     **/
    @RequestMapping(value = "/getColumnList", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<List<Column>>> getColumnList(@RequestBody Column column){
        return () -> {
            ResultDataStruct<List<Column>> resultDataStruct = new ResultDataStruct<>();
            try {
                resultDataStruct.setData(columnService.getColumnList(column));
                resultDataStruct.setMessage("成功");
                resultDataStruct.setCode(200);
            }catch (CustomException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("查询分类出错",e);
                resultDataStruct.setMessage("查询分类出错");
                resultDataStruct.setCode(400);
            }
            return resultDataStruct;
        };
    }


    /**
     * @author chenfangzhou
     * @Description 获取该站点下所有的分类数据
     * @return
     **/
    @RequestMapping(value = "/getAllColumnList", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<List<Column>>> getAllColumnList(@RequestBody Column column) throws Exception{
        return () ->{
            ResultDataStruct<List<Column>> resultDataStruct = new ResultDataStruct<>();
            try {
                resultDataStruct.setData(columnService.getAllColumnList(column));
                resultDataStruct.setMessage("成功");
                resultDataStruct.setCode(200);
            }catch (CustomException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("获取站点下所有的分类数据出错",e);
                resultDataStruct.setMessage("获取站点下所有的分类数据出错");
                resultDataStruct.setCode(400);
            }
            return resultDataStruct;
        };
    }


    /**
     * @author yaosy
     * @Description 根据站点及模板id获取绑定的分类列表数据
     * @return
     **/
    @RequestMapping(value = "/getColumnListByTempletIds", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<List<Column>> getColumnListByTempletIds(@RequestBody Column column) throws Exception{
        return () -> columnService.getColumnListByTempletIds(column);
    }

    /**
     * @author Mr.zheng
     * @Description getColumnByTempletId 根据模板ID和站点ID去获取一条分类模拟数据
     * @Date 17:32 2018/8/16
     **/
    @RequestMapping(value = "/getColumnByTempletId", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Column>> getColumnByTempletId(@RequestBody Column column) throws Exception{
        return () -> {
            ResultDataStruct<Column> resultDataStruct = new ResultDataStruct<>();
            try {
                resultDataStruct.setData(columnService.getColumnByTempletId(column));
                resultDataStruct.setMessage("成功");
                resultDataStruct.setCode(200);
            }catch (CustomException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("根据模板ID和站点ID去获取一条分类模拟数据出错",e);
                resultDataStruct.setMessage("根据模板ID和站点ID去获取一条分类模拟数据出错");
                resultDataStruct.setCode(400);
            }
            return resultDataStruct;
        };
    }

}
